<?php

namespace App\Http\Controllers;

use App\Assorts;
use App\Boxs;
use App\Goods;
use Carbon\Carbon;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Http\Response;


class StoreController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($keyworld='')
    {
        $boxs = Boxs::where('state','<=','6')->where(function($query) use ($keyworld){
                $query->whereRaw('DATE_FORMAT(created_at,\'%Y%m%d%h%i%s\')=\'' . $keyworld . '\'')
                    ->orWhere(function($query) use ($keyworld){
                        $query->where('express_out_no',$keyworld);
                    });
            })->get();
        $historyboxs =Boxs::whereRaw('DATE_FORMAT(updated_at,\'%Y%m%d\')=\''.Carbon::now()->format('Ymd').'\'')->where('state','>=','6')->orderBy('updated_at','desc')->get();

        return view('system.store.regular',compact(['boxs','historyboxs']));
    }

    public function history($keyworld='')
    {
        $historyboxs = Boxs::where('state', '>=', '6')->orwhereRaw('DATE_FORMAT(created_at,\'%Y%m%d%h%i%s\')=\'' . $keyworld . '\'')->orWhere('express_out_no', $keyworld)->orderBy('updated_at', 'desc')->paginate(15);

        return view('system.store.history',compact('historyboxs'));
    }

    public function store($orderno,$id,$state)
    {
        $assort = Assorts::where('id',$id)->where('state','<','6')->first();

        $keeper = \Auth::user()->id;    //当前登录用户

        if($state==="8") {//污损不增加库存
            $assort->state = $state;
            $assort->keeper = $keeper;
            $assort->save();
        }elseif($state==="7"){//出售不减库存也不加
            $assort->state = $state;
            $assort->keeper = $keeper;
            $assort->save();
        }elseif($state==="6"){//回库增加库存
            $good = Goods::where('id',$assort->goods_id)->first();
            $good->number = $good->number +1;
            $good->save();
            $assort->state=$state;
            $assort->keeper = $keeper;
            $assort->save();
        }

        $assorts = Assorts::where('orderno',$orderno)->where('state','<','6')->get();
        if(count($assorts)<1) {
            $box = Boxs::where('orderno',$orderno)->first();

            $count = Assorts::where(['orderno'=>$orderno,'state'=>7])->get();
            if(count($count)>1){
                $total= Assorts::where(['orderno'=>$orderno,'state'=>7])->groupBy('orderno')->sum('discount');
            }else{
                $total= Assorts::where(['orderno'=>$orderno,'state'=>7])->groupBy('orderno')->sum('price');
            }

            $box->state='6';//虽然每件衣服状态不同，可是盒子单独计为返回。
            $box->total=$total;
            $box->keeper = $keeper;
            $box->save();
        }

        return response()->json(array('state' => 'true','count'=>count($assorts)));
    }

    public function allstore($orderno,$state)
    {
        $iserror=false;

        $assorts = Assorts::where('orderno',$orderno)->where('state','<','6')->get();

        $keeper = \Auth::user()->id;

        foreach ($assorts as $assort) {

            $good = Goods::where('id',$assort->goods_id)->first();

            if($good!=null){
                $good->number = $good->number +1;
                $good->save();
                $assort->state=$state;
                $assort->keeper = $keeper;
                $assort->save();
            }else{
                $iserror =true;
            }
        }

        $box = Boxs::where('orderno',$orderno)->first();
        if(!$iserror) {

            $count = Assorts::where(['orderno'=>$orderno,'state'=>7])->get();
            if(count($count)>1){
               $total= Assorts::where(['orderno'=>$orderno,'state'=>7])->groupBy('orderno')->sum('discount');
            }else{
               $total= Assorts::where(['orderno'=>$orderno,'state'=>7])->groupBy('orderno')->sum('price');
            }

            $box->state=$state;
            $box->keeper = $keeper;
            $box->total=$total;
            $box->save();
        }

        return response()->json(array('state' => 'true','count'=>0));
    }
}
